package controllers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"TeacherManager/models"
	"github.com/gin-contrib/sessions"
)

/**
* Created by LONG  on 2018/5/26.
*/

var colors = []string{"bg-orange",
	"bg-aqua",
	"bg-blue",
	"bg-red",
	"bg-light-blue",
	"bg-green",
	"bg-navy",
	"bg-teal",
	"bg-olive",
	"bg-fuchsia",
	"bg-lime"}

func AdminIndex(c *gin.Context) {
	user, _ := c.Get(CONTEXT_USER_KEY)
	u, _ := user.(*models.User)
	c.HTML(http.StatusOK, "admin/index.html", gin.H{
		"classCount":  models.CountClass(u.ID),
		"examCount":   models.CountExam(u.ID),
		"user":        user,
		"comments":    models.MustListUnreadComment(),
		"noticeCount": models.CountNotice(),
	})
}

func LogoutGet(c *gin.Context) {
	s := sessions.Default(c)
	s.Clear()
	s.Save()
	c.Redirect(http.StatusSeeOther, "/signin")
}

func ProfileGet(c *gin.Context) {
	sessionUser, exists := c.Get(CONTEXT_USER_KEY)
	if exists {
		c.HTML(http.StatusOK, "admin/profile.html", gin.H{
			"user":     sessionUser,
			"comments": models.MustListUnreadComment(),
		})
	}
}
func ProfileUpdate(c *gin.Context) {
	//avatarUrl := c.PostForm("avatarUrl")
	email := c.PostForm("email")
	nickName := c.PostForm("nickName")
	sessionUser, _ := c.Get(CONTEXT_USER_KEY)
	if user, ok := sessionUser.(*models.User); ok {
		err := user.UpdateProfile(email, nickName)
		if err == nil {
			c.JSON(http.StatusOK, gin.H{
				"succeed": true,
				"user":    models.User{Email: email, NickName: nickName},
			})
		} else {
			c.JSON(http.StatusOK, gin.H{
				"succeed": false,
				"message": err.Error(),
			})
		}
	}
}
